<template>
  <div>
    <div>
      <el-input
        size="mini"
        class="addPosInput"
        placeholder="添加职位..."
        prefix-icon="el-icon-plus"
        v-model="pos.name"
        @keydown.enter.native="addPosition"
      >
      </el-input>
      <el-button
        icon="el-icon-plus"
        type="primary"
        size="mini"
        @click="addPosition"
        >添加</el-button
      >
    </div>
    <el-button
      size="mini"
      type="danger"
      style="margin-top: 10px"
      :disabled="multipleSelection == 0"
      @click="deleteBatchBut"
      >批量删除</el-button
    >
    <div class="posManaTable">
      <el-table
        :data="positions"
        border
        stripe
        style="width: 40%"
        size="mini"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column prop="id" label="编号" width="55"> </el-table-column>
        <el-table-column prop="name" label="职位名称" width="120">
        </el-table-column>
        <el-table-column prop="createDate" label="创建时间" width="250">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog title="修改职称" :visible.sync="dialogVisible" width="30%">
      <div>
        <el-row>
          <el-col :span="5" class="el_col">职位名称:</el-col>
          <el-col :span="19"
            ><el-input size="small" v-model="updataPos.name"></el-input
          ></el-col>
        </el-row>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelDialog" size="mini">取 消</el-button>
        <el-button type="primary" @click="doUpdatePos" size="mini"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  components: {},
  data() {
    return {
      pos: {
        name: "",
      },
      updataPos: {
        id: "",
        name: "",
      },
      deleteBatchFlag: false,
      dialogVisible: false,
      positions: [],
      multipleSelection: [],
    };
  },
  methods: {
    deleteBatchBut() {
      let ids = [];
      this.multipleSelection.forEach((item) => {
        ids.push(item.id);
      });
      this.$confirm(
        "此操作将永久删除【" + ids.length + "】条记录, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          this.deleteRequset(
            "/system/basic/pos/deletePositionByIds/",
            ids
          ).then((resp) => {
            this.initPosition();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    cancelDialog() {
      this.dialogVisible = false;
    },
    addPosition() {
      if (!this.pos.name) {
        this.$message.error("职位名称不能为空!");
        return false;
      }
      this.postRequset("/system/basic/pos/addPosition", this.pos).then(
        (resp) => {
          if (resp) {
            this.initPosition();
            this.pos.name = "";
          }
        }
      );
    },
    doUpdatePos() {
      this.putRequset("/system/basic/pos/updatePosition", this.updataPos).then(
        (resp) => {
          if (resp) {
            this.initPosition();
            this.dialogVisible = false;
          }
        }
      );
    },
    handleEdit(index, data) {
      Object.assign(this.updataPos, data);
      this.dialogVisible = true;
    },
    handleDelete(index, data) {
      this.$confirm(
        "此操作将永久删除【" + data.name + "】职位, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          this.deleteRequset(
            "/system/basic/pos/deletePositionById/" + data.id
          ).then((resp) => {
            this.initPosition();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    initPosition() {
      this.getRequset("/system/basic/pos/getAllPosition").then((resp) => {
        if (resp) {
          this.positions = resp;
        }
      });
    },
  },
  mounted() {
    this.initPosition();
  },
};
</script>
<style>
.addPosInput {
  width: 300px;
  margin-right: 8px;
}
.posManaTable {
  margin-top: 10px;
}
.el_col {
  text-align: center;
  line-height: 32px;
}
</style>